"""
用户设置数据模式

定义用户设置相关的Pydantic模型，用于API请求和响应的数据验证。

主要功能：
1. 用户设置数据验证
2. API请求/响应模式定义
3. 设置更新和创建模式
4. 类型安全和数据验证

作者: LLM Chat System
版本: 1.0.0
"""

from typing import Optional, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field


class UserSettingsBase(BaseModel):
    """用户设置基础模式"""
    
    # 默认模型设置
    default_model_id: Optional[int] = Field(None, description="默认聊天模型ID")
    title_generation_model_id: Optional[int] = Field(None, description="标题生成模型ID")
    
    # 聊天设置
    auto_title_generation: bool = Field(True, description="是否启用自动标题生成")
    title_generation_threshold: int = Field(3, ge=1, le=20, description="标题生成阈值，对话消息数达到此值时生成标题")
    
    # 界面设置
    theme: str = Field("light", description="主题设置：light/dark")
    language: str = Field("zh-CN", description="界面语言设置")
    font_size: int = Field(14, ge=10, le=24, description="字体大小设置")
    
    # 其他设置
    auto_save: bool = Field(True, description="是否自动保存对话")
    max_conversations: int = Field(50, ge=10, le=1000, description="最大对话数量限制")
    
    # 思维链设置
    enable_chain_of_thought: bool = Field(False, description="是否启用思维链功能")
    
    # 扩展设置
    extra_settings: Optional[Dict[str, Any]] = Field(None, description="扩展设置")


class UserSettingsCreate(UserSettingsBase):
    """创建用户设置模式"""
    user_id: int = Field(..., description="用户ID")


class UserSettingsUpdate(BaseModel):
    """更新用户设置模式"""
    
    # 默认模型设置
    default_model_id: Optional[int] = Field(None, description="默认聊天模型ID")
    title_generation_model_id: Optional[int] = Field(None, description="标题生成模型ID")
    
    # 聊天设置
    auto_title_generation: Optional[bool] = Field(None, description="是否启用自动标题生成")
    title_generation_threshold: Optional[int] = Field(None, ge=1, le=20, description="标题生成阈值")
    
    # 界面设置
    theme: Optional[str] = Field(None, description="主题设置")
    language: Optional[str] = Field(None, description="界面语言设置")
    font_size: Optional[int] = Field(None, ge=10, le=24, description="字体大小设置")
    
    # 其他设置
    auto_save: Optional[bool] = Field(None, description="是否自动保存对话")
    max_conversations: Optional[int] = Field(None, ge=10, le=1000, description="最大对话数量限制")
    
    # 思维链设置
    enable_chain_of_thought: Optional[bool] = Field(None, description="是否启用思维链功能")
    
    # 扩展设置
    extra_settings: Optional[Dict[str, Any]] = Field(None, description="扩展设置")


class UserSettingsResponse(UserSettingsBase):
    """用户设置响应模式"""
    
    id: int = Field(..., description="设置记录ID")
    user_id: int = Field(..., description="用户ID")
    created_at: datetime = Field(..., description="设置创建时间")
    updated_at: datetime = Field(..., description="设置最后更新时间")
    
    class Config:
        from_attributes = True


class UserSettingsWithModels(UserSettingsResponse):
    """包含模型信息的用户设置响应模式"""
    
    default_model_name: Optional[str] = Field(None, description="默认模型名称")
    title_generation_model_name: Optional[str] = Field(None, description="标题生成模型名称")


class TitleGenerationRequest(BaseModel):
    """标题生成请求模式"""
    
    conversation_id: int = Field(..., description="对话ID")
    messages: Optional[list] = Field(None, description="对话消息列表（可选，如不提供则从数据库获取完整历史）")
    model_id: Optional[int] = Field(None, description="指定的标题生成模型ID")


class TitleGenerationResponse(BaseModel):
    """标题生成响应模式"""
    
    success: bool = Field(..., description="是否成功")
    title: Optional[str] = Field(None, description="生成的标题")
    message: str = Field(..., description="响应消息")
    model_used: Optional[str] = Field(None, description="使用的模型名称")
